package ynu.edu.model.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import ynu.edu.common.domain.R;
import ynu.edu.model.domain.GeneratedImage;
import ynu.edu.model.service.ImagePredictService;
import ynu.edu.model.utils.AliOssUtil;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/image")
@RequiredArgsConstructor
@Api(tags = "StyleGan3图像生成接口")
public class ImagePredictController {

    private final ImagePredictService imagePredictService;

    @PostMapping("/predictImage")
    @ApiOperation("图像生成")
    public R<GeneratedImage> predictImage(@RequestBody Map<String, Object> params) {
        log.info("接收到请求：seed={}, truncationPsi={}", params.get("seed"), params.get("truncationPsi"));
        return R.ok(imagePredictService.saveImage(params));
    }

    @GetMapping("/history")
    @ApiOperation("获取生成历史")
    public R<List<GeneratedImage>> getHistory(@RequestParam(defaultValue = "10") int limit) {
        return R.ok(imagePredictService.findRecent(limit));
    }

    @GetMapping("/userImage")
    @ApiOperation("获取用户图像")
    public R<GeneratedImage> getUserImage(long userId) {
        return R.ok(imagePredictService.getUserImage(userId));
    }

    @PostMapping("/uploadAvatar")
    @ApiOperation("上传用户头像")
    public R<GeneratedImage> uploadAvatar(@RequestParam("file") MultipartFile file) {
        return R.ok(imagePredictService.uploadAvatar(file));
    }

    @PostMapping("/generate")
    @ApiOperation("生成图像")
    public R<GeneratedImage> generate(@RequestBody Map<String, Object> params) {
        log.info("汉字生成：char:{},step:{}",  params.get("char"), params.get("step"));
        return R.ok(imagePredictService.generate(params));
    }

}